package com.cskaoyan.market.controller.admin;


import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.service.admin.MarketOrderService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @MethodName 商场管理模块订单部分请求控制
 * @Description list订单表    detail订单详情	refund退款->修改订单状态	channel查看快递公司	ship发货->修改订单状态	 delete物理删除/逻辑删除
 * @Author M
 * @Date 16:21 2023/5/7
 **/
@RequiresAuthentication
@RestController
@RequestMapping("admin/order")
public class AdminOrderController {

    @Autowired
    MarketOrderService adminManagerOrderService;

    // /admin/order/detail?id=1

    // 查看快递公司
    @GetMapping("channel")
    public Object channel() throws IOException {
        List<MarketExpress> marketChannel = MarketExpressExample.marketExpresses;
        Object o = ResponseUtil.ok(marketChannel);
        // 返回请求
        return o;
    }

    // 商品详情页
    @GetMapping("detail")
    public Object detail(String id) throws IOException {

        // 通过订单id查询到用户id，创建总map
        Map<String, Object> result = new HashMap<>();
        // order详情
        MarketOrder order = adminManagerOrderService.getMarketOrder(id);
        // 获取user详情
        int userId = adminManagerOrderService.getUserIdByOrderId(id);
        MarketUser marketUser = adminManagerOrderService.getUserByOrderId(userId);
        Map<String, Object> user = new HashMap<>();
        user.put("nickname", marketUser.getNickname());
        user.put("avatar", marketUser.getAvatar());
        // 获取orderGoods详情
        List<MarketOrderGoods> orderGoods = adminManagerOrderService.getMarketOrderGoods(id);
        System.out.println("orderGoods = " + orderGoods);
        // 添加字段
        result.put("orderGoods", orderGoods);
        result.put("user", user);
        result.put("order", order);
        return ResponseUtil.ok(result);
    }

    // 订单详情 market_order表
    @GetMapping("list")
    public Object List(String page, String limit, String sort, String order, String userId, String id, String orderSn, String[] orderStatusArray, String start, String end) {
        // 添加默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            //做校验，保证page和limit必须存在，且是数字
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        } catch (Exception e) {
            return ResponseUtil.badArgument();
        }

        List<MarketOrder> marketOrders = adminManagerOrderService.getMarketOrderList(orderStatusArray, userId, id, orderSn, start, end, sort, order, pageInt, limitInt);
        Object o = ResponseUtil.okList(marketOrders);
        // 返回请求
        return o;
    }

    // 修改订单状态 发货
    @PostMapping("ship")
    public Object ship(@RequestBody Map<String, String> map) throws IOException {
        // 获取请求参数
        Integer orderId = Integer.valueOf(map.get("orderId"));
        // 快递
        String shipChannel = map.get("shipChannel");
        // 快递单号
        String shipSn = map.get("shipSn");
        // 校验参数
        if ("".equals(shipChannel) || "".equals(shipSn)) {
            // 快递单号与快递公司为空
            Object o = ResponseUtil.badArgument();
            return o;
        }
        // 值都不是null
        // 根据返回参数判断是否删除成功，通过orderId添加快递与快递单号
        int row = adminManagerOrderService.shipOrder(orderId, shipChannel, shipSn);
        if (row != 1) {
            // 修改失败
            Object o = ResponseUtil.badArgumentValue();
            return o;
        }
        // 修改成功
        Object o = ResponseUtil.ok();
        return o;
    }

    // 逻辑删除订单
    @PostMapping("delete")
    public Object delete(@RequestBody Map<String, String> map) throws IOException {
        // 获取请求参数
        Integer orderId = Integer.valueOf(map.get("orderId"));
        // 根据返回参数判断是否删除成功
        int row = adminManagerOrderService.deleteOrder(orderId);
        if (row != 1) {
            // 删除失败
            Object o = ResponseUtil.wrongOrderCannotDeleted();
            return o;
        }
        // 删除成功
        Object o = ResponseUtil.ok();
        return o;
    }

    /**
     * @MethodName refund
     * @Description 实现确认退款操作，对订单状态、退款金额、确认退款时间、退款备注进行修改
     * @param: req
     * @param: resp
     * @Author M
     * @Date 13:55 2023/5/8
     **/
    @PostMapping("refund")
    public Object refund(@RequestBody Map<String, String> map) throws IOException {
        Integer orderId = Integer.valueOf(map.get("orderId"));
        String refundMoney = map.get("refundMoney");
        BigDecimal refundAmount = new BigDecimal(refundMoney);
        // 对信息进行判空校验
        // 退款功能,通过orderId找到相应的order表把表中订单状态修改为  203: '已退款'
        // 退款成功返回1 ，否则失败
        int marketRefund = adminManagerOrderService.refund(orderId, refundAmount);
        if (marketRefund != 1) {
            // 修改失败
            Object o = ResponseUtil.badArgument();
            return o;
        }
        // 修改成功
        Object o = ResponseUtil.ok();
        return o;
    }
}
